package com.cy.pj.sys.controller;

import java.util.Map;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.cy.pj.common.bo.JsonResult;
import com.cy.pj.sys.pojo.SysUser;
import com.cy.pj.sys.service.SysUserService;

@RestController
@RequestMapping("/user/")
public class SysUserController {
	@Autowired
	private SysUserService sysUserService;
	
	@RequestMapping("doUpdatePassword")
	public JsonResult doUpdatePassword(String pwd,String newPwd,String cfgPwd) {
		sysUserService.updatePaword(pwd, newPwd, cfgPwd);
		return new JsonResult("update ok");
	}
	
	@RequestMapping("doLogin")
	public JsonResult doLogin(String username,String password,Boolean isRememberMe) {
		//获取Subject对象
		Subject subject = SecurityUtils.getSubject();
		//将参数封装到Token中
		UsernamePasswordToken token = new UsernamePasswordToken(username, password);
		token.setRememberMe(isRememberMe);
		//进行登录验证
		//分析:1->token会传给shiro的SecurityManager中,2-->SecurityManager会将token传递给认证管理器
		//3-->认证管理器会将token传递给realm
		subject.login(token);
		return new JsonResult("login ok");
	}
	
	@RequestMapping("doFindPageObjects")
	public JsonResult doFindPageObjects(String username,Integer pageCurrent) {
		return new JsonResult(sysUserService.findPageObject(username, pageCurrent));
	}
	
	@RequestMapping("doValidById")
	public JsonResult doValidById(Integer id,Integer valid) {
		sysUserService.validById(id, valid);
		return new JsonResult(" update ok");
	}
	
	@RequestMapping("doSaveObject")
	public JsonResult doSaveObject(SysUser sysUser,Integer... roleIds) {
		System.out.println(sysUser+","+roleIds);
		sysUserService.doSaveObject(sysUser, roleIds);
		return new JsonResult("insert ok");
	}
	
	@RequestMapping("doUpdateObject")
	public JsonResult doUpdateObject(SysUser sysUser,Integer... roleIds) {
		sysUserService.doUpdateObject(sysUser, roleIds);
		return new JsonResult("update ok");
	}
	
	@RequestMapping("doFindObjectById")
	public JsonResult doFindObjectById(Integer id) {
		Map<String, Object> doFindObjectById = sysUserService.doFindObjectById(id);
		return new JsonResult(doFindObjectById);
	}
	
}